<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <template v-if="isDeleteShow">
        <span @click="clearAll">全部删除</span>
        <span @click="isDeleteShow = false">完成</span>
      </template>
      <van-icon name="delete" v-else @click="isDeleteShow = true"/>
    </van-cell>
    <van-cell :title="value" v-for="(value, index ) in searchhistories" :key="index" @click="onClickHistory(value, index)">
      <van-icon name="close" v-if="isDeleteShow"/>
    </van-cell>

  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchhistories: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      isDeleteShow: false, // 是否显示删除按钮
      searchhistoriesClone: this.searchhistories
    }
  },
  methods: {
    onClickHistory (value, index) {
      if (this.isDeleteShow) {
        //  删除模式
        this.searchhistoriesClone.splice(index, 1)
      } else {
        // 点击搜索历史，跳转搜索结果
        this.$emit('search', value)
      }
    },
    clearAll () {
      this.$emit('clearAll')
    }
  }
}
</script>

<style scoped lang="less"></style>
